package com.jxchexie.admin;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.afollestad.materialdialogs.MaterialDialog;
import com.blankj.utilcode.utils.TimeUtils;
import com.jxchexie.adapter.DangerAdminAdapter;
import com.jxchexie.adapter.HolidayAdminAdapter;
import com.jxchexie.api.Api;
import com.jxchexie.model.Holiday;
import com.jxchexie.model.ResponseHolidayRecord;
import com.jxchexie.model.ResponseSigninRecord;
import com.jxchexie.model.SignIn;
import com.jxchexie.signin.MainActivity;
import com.jxchexie.signin.R;
import com.jxchexie.utils.Constant;
import com.jxchexie.utils.JsonUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Response;

public class AdminDangerActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private Toolbar dangerUser;
    private MaterialDialog md;
    private ArrayList<SignIn> datas = new ArrayList<>();
    private ResponseSigninRecord responseSigninRecord;
    private DangerAdminAdapter adapter;
    private TextView editText;
    private String ed;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin_danger);
        initView();
        initEvent();
    }

    private void initEvent() {
        dangerUser.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AdminDangerActivity.this.finish();/*关闭当前页*/
            }
        });
    }

    private void initView() {
        dangerUser = (Toolbar) findViewById(R.id.dangerAdminToolbar) ;
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view_danger);
        /*显示提示正在登录对话框*/
        md=new MaterialDialog.Builder(AdminDangerActivity.this)
                .title("提示")
                .content("数据加载中...")
                .progress(true, 0)
                .show();
        //
        setAdapter();
        initData();
    }
    private void setAdapter(){
        adapter=new DangerAdminAdapter(datas,AdminDangerActivity.this);
        LinearLayoutManager mLayoutManager=new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    }
//    private void show(){
//        AlertDialog.Builder builder = new AlertDialog.Builder(AdminDangerActivity.this);
//        builder.setTitle("输入正常的标准：");//设置标题
//        builder.setIcon(android.R.drawable.ic_dialog_info);
//        builder.setView(editText);
//        //设置一个确定按钮
//        //final String text;
//        //text = msg;
//        builder.setPositiveButton("查询", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                ed = editText.getText().toString();
//                dialog.dismiss();
//            }
//        });
//        builder.show();
//    }
    private void initData(){
        /*获取当前时间*/
        java.text.SimpleDateFormat ss = new java.text.SimpleDateFormat("yyyy-MM-dd");
        String date= TimeUtils.getCurTimeString(ss);
        HashMap<String, String> params = new HashMap<>();
        params.put("date", date);
        JSONObject jsonObject = new JSONObject(params);
        /*获取请假记录*/
        OkGo.post(Api.DANGER_USER)//
                .upJson(jsonObject.toString())
                .tag(this)//
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        /*关闭提示框*/
                        md.dismiss();

                        responseSigninRecord=new ResponseSigninRecord();
                        responseSigninRecord= JsonUtils.fromJson(s,ResponseSigninRecord.class);
                        if(responseSigninRecord.getStatus().equals(Constant.SUCCESS)){
                            datas.addAll(responseSigninRecord.getMsg());
                            Log.v("zj",String.valueOf(datas.size()));
                            if(datas==null || datas.size()<=0){
                                Toast.makeText(AdminDangerActivity.this,"无危险用户...",Toast.LENGTH_SHORT).show();
                            }else{
                                Log.v("zj","调用notify");
                                adapter.notifyDataSetChanged();
                            }
                        }
                    }
                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        //md.dismiss();
                        Toast.makeText(AdminDangerActivity.this,"请稍后重试！",Toast.LENGTH_SHORT).show();
                    }
                });
    }
}